const app = getApp()
Page({
    data: {
        total:0,
        rows:[],
        query:{  
        key:'',  //搜索关键字 
        pageNum:1,
        pageSize:10
        },
       
      TopNavs:[
          {
            id : 1,
            title : '综合'
          },
          {
            id : 2,
            title : '销量'
          },
          {
            id : 3,
            title : '价格'
          },
          {
            id : 4,
            title : '全部品牌'
          }
        ],

        currentNav:1,
        currentIndex:0,
        navHeight: '',
        statusBarHeight: 0, // 状态栏高度
        searchMarginTop: 0, // 搜索框上边距
        searchWidth: 0, // 搜索框宽度
        searchHeight: 0, // 搜索框高度
    },
    inputKey(e){
        this.data.query.key = e.detail.value
    },
    refresh(next){
        this.data.query.pageNum = 1
        this.data.rows = []
        this.data.total = 0
        this.getList(next)
    },
    onLoad : function(options){
        
        this.setData({
            key:options.key || ''
        })
        this.getList()
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor:'#000000'
        })
         let menuButtonInfo = wx.getMenuButtonBoundingClientRect()
        const { top, width, height, right } = menuButtonInfo
        wx.getSystemInfo({
          success: (res) => {
            const { statusBarHeight } = res
            const margin = top - statusBarHeight
            this.setData({
              statusBarHeight: statusBarHeight,
              navHeight: (height + statusBarHeight + (margin * 2)),
              searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
              searchHeight: height,  // 与胶囊按钮同高
              searchWidth: right - width - 100 // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度，再减去30两边间距
            })
          }, 

          

        })
       

        

    },
 getList(next){

        wx.get('/products',this.data.query).then(res => {
            console.log(res)
               res.rows.forEach((item,index) => {
                item.img = wx.baseURL + item.productImgs.split(',')[0]
            })
         this.setData({
            total :res.total,
            rows:this.data.rows.concat(res.rows),
            })
         if(typeof next == 'function') next()
         })
    },
      
         
    //         this.setData({
    //             ...res
    //         })
    //     })
    // },
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.refresh(() => {
            wx.stopPullDownRefresh()
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.query.pageNum ++
        if(this.data.rows.length < this.data.total) this.getList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})